﻿
// Condition: Write a program that fills and prints a matrix of size (n,n) with particular structure.

using System;



class Matrix_Var4
{
    static void Main()
    {
        // 1.initial statements
        Console.Write("Enter the size of square matrix, n= ");
        int n = int.Parse(Console.ReadLine());
        int count = 1;
        int[,] matrix = new int[n, n];
        string direction = "down";
        int col=-1;
        int row=0;

        // 2. fill the elements by going through all directions while count=n*n

        while (count<=n*n)
        {
            
            if ( direction=="down" )
            {
                col = col + 1;
                for (row = col; row < (n-col); row++)
                {
                    matrix[row, col] = count;
                    count++;
                }
                direction = "right";
            }
            else if (direction == "right")
            {
                row = row - 1;
                for (col = col+1; col < row+1; col++)
                {
                    matrix[row, col] = count;
                    count++;
                }
                direction = "up";
            }

            else if (direction == "up")
            {
                col = col - 1;
                for ( row = row-1; row >= n-1-col; row--)
                {
                    matrix[row, col] = count;
                    count++;
                }
                direction = "left";
            }
           
            else if (direction == "left")
            {
                row = row + 1;
                for ( col = col-1; col >= row+1; col--)
                {
                    matrix[row, col] = count;
                    count++;
                    
                }
                direction = "down";
            }
        }

        // 3. print the matrix[n,n]

        for (int irow = 0; irow < n; irow++)
        {
            for (int icol = 0; icol < n; icol++)
            {
                Console.Write(matrix[irow, icol] + " ");
            }
            Console.WriteLine();
        }
        Console.WriteLine();


    }
}

